Skip to content

feat(pdf-codec): a document-wide decode budget (maxDecodedBytes) - #1815

Merged
Mearman merged 6 commits into
mainfrom
feat/pdf-codec-decode-budget
Nov 25, 2026
Merged

Mearman merged 6 commits into
mainfrom
feat/pdf-codec-decode-budget

Conversation

@Mearman

@Mearman Mearman commented Nov 16, 2026

Copy link
Copy Markdown
Member

Fixes #1814

readPdf, renderPdfPage and openPdfDocument take maxDecodedBytes, one budget charged by every decode the document reaches. The first decode that would pass it throws PdfDecodeBudgetError (a PdfParseError, code pdf/decode-budget-exceeded, carrying maxDecodedBytes, decodedBytes already spent, the stream's objectNumber and its filters). documents.js forwards the option from every pdf conversion and read and re-exports the error.

Decisions

  • One decode path. PdfObjectResolver gains decodeStream(stream, sink) and chargeDecodedBytes(stream, n), and every reader decodes through it instead of importing filters.ts's decodeStream. Only document.ts and xref.ts (which charge the budget themselves) import it, and a test fails if any other module starts to, so a new decode site cannot bypass the budget.
  • Sites covered: cross-reference streams, object streams (on first resolve and during cross-reference recovery), page content streams and /Contents arrays, form XObjects, image XObjects and inline images, /SMask, /Indexed lookups, /Metadata, embedded files, /ToUnicode, /CIDToGIDMap (font read and raster), and font programs for encoding, PostScript name, digest and raster outlines.
  • What counts: every filter stage's output, so [/ASCII85Decode /FlateDecode] pays for both. A predictor is not charged again, since its output is never longer than the stage it rewrites. A stream decoded twice is charged twice (a program two fonts share costs two decodes); the existing per-stream name and digest caches still decode once.
  • Stopping early: Flate inflates through fflate's streaming inflaters, fed in input slices sized to what the remainder can absorb at DEFLATE's maximum expansion (1032 bytes per input byte), keeping inflateTolerant's four recovery tiers. LZW, RunLength and ASCII85 throw at the first byte past the limit. ASCIIHex is refused from its exact size before allocating. CCITTFax is refused from /Rows (or, with none, the most rows its input bits can encode), JBIG2 from /Width and /Height (refused without them), JPEG 2000 from its sample planes before decodeJpeg2000 allocates them (new onSampleAllocation option), and a JPEG renderPdfPage hands to a rasteriser from its frame header. readPdf passes JPEGs through undecoded, so they cost nothing there.
  • A refusal ends the read. readJpeg2000Image rethrows the budget error instead of degrading to "skip this image", and once one decode is refused the budget is spent, so later decodes are refused even through a caller that caught the first error.
  • Encryption: object streams and every resolved stream are decrypted by fetch() before decoding, so the budget charges plaintext decoding. Cross-reference streams are never encrypted. An object stream found by recovery is decoded before the decryptor exists, as it already was, and is charged for whatever that produces.
  • Opt-in. With no budget, decodeStream calls the same unbounded decoders as before (inflateTolerant, not the bounded ladder), so output is unchanged.
  • documents.js boundary: UnifiedConversionOptions, PdfToDocumentOptions, ReadNativeDocumentTreeOptions and ReadDocumentMetadataOptions take maxDecodedBytes and forward it like pages and surfaces: only to the hop that reads the caller's own pdf, never an intermediate pdf a route renders.

Not bounded

Work that produces no decoded bytes: content interpretation, text layout and PNG encoding, which scale with page and content-stream sizes a caller can measure beforehand. JBIG2's intermediate bitmaps stay bounded by its existing per-bitmap ceiling rather than charged.

Tests

  • src/decode-budget.test.ts and src/decode-budget-sites.test.ts: each filter refuses a small stream expanding past the budget, and decodes what fits byte-for-byte as without one (seeded sizes, exact-fit and one-short budgets); inflateTolerantBounded against inflateTolerant over 200 seeded valid, truncated, whitespace-prefixed and raw streams; one document per site whose only large stream is at that site, which reads without a budget and throws naming that object with one (reverting any site's charge makes its test fail); shared font programs charged twice; JPEG 2000 samples; encrypted object streams; renderPdfPage's content, font-program, CIDToGIDMap and JPEG charges; every existing document fixture reads identically with an unlimited budget and with none.
  • src/convert/from-pdf-decode-budget.test.ts (documents.js): each pdf conversion and read refuses with PdfDecodeBudgetError, converts within the budget, and an intermediate pdf is not charged.
  • test/workers/pdf-codec.test.ts: the refusal inside workerd.

@Mearman
Mearman marked this pull request as ready for review November 16, 2026 11:37
@Mearman
Mearman force-pushed the feat/pdf-codec-decode-budget branch from ffeadca to 11c943f Compare November 24, 2026 21:08
@claude

claude Bot commented Nov 24, 2026

Copy link
Copy Markdown

⚠️ PR Steward hasn't started: your organization has no extra usage available to pay for it, so it isn't working on this pull request.

If your organization's extra usage balance is empty or was never set up, an Owner of your Claude organization can add extra usage credits at claude.ai/admin-settings/usage. If your organization's monthly spend limit for extra usage is $0 or was reached, an Owner can raise it on the same page. If neither applies, contact Anthropic support.

Once that's fixed, PR Steward starts at the next push to this pull request.

Settings

readPdf, renderPdfPage and openPdfDocument take maxDecodedBytes, one budget for every
decode the document reaches: cross-reference and object streams (recovery included),
content streams, form XObjects, font programs, ToUnicode and CIDToGIDMap streams,
images, inline images, soft masks, Indexed lookups, metadata and embedded files.
Every reader now decodes through the resolver, so no site can decode outside it.

The first decode that would pass the budget throws PdfDecodeBudgetError, a
PdfParseError carrying the budget, the bytes already spent, the stream's object
number and its filters. Flate inflates in slices sized to what the remainder can
absorb, LZW, RunLength and ASCII85 stop at the first byte past it, and the
whole-image decoders (CCITTFax, JBIG2, JPEG 2000, a rasterised JPEG) are charged
their declared output before they decode. decodeStream takes the budget directly.

Without the option nothing is charged and every decoder runs its existing code.
…conversion

UnifiedConversionOptions, PdfToDocumentOptions, ReadNativeDocumentTreeOptions and
ReadDocumentMetadataOptions take maxDecodedBytes and hand it to readPdf, so a pdf
conversion that would decode too much throws PdfDecodeBudgetError, now re-exported,
rather than a generic failure. Like pages and surfaces it reaches only the hop that
reads the caller's own pdf, never an intermediate pdf a route renders.
…nflate size hint under a budget

openPdfImages, which landed on main after this branch started, opened its
document without a budget, so a one-at-a-time image read escaped
maxDecodedBytes. It now takes the option and charges listing and every decode
against the one budget.

The expected-length hint main added to decodeStream moves into
DecodeStreamOptions beside the budget, and the per-image colour-space
resolver passes decodeStream and chargeDecodedBytes through.
@Mearman
Mearman force-pushed the feat/pdf-codec-decode-budget branch from 11c943f to 1b5a3b6 Compare November 25, 2026 09:16
@Mearman
Mearman added this pull request to the merge queue Nov 25, 2026
Merged via the queue into main with commit 5f7f245 Nov 25, 2026
36 checks passed
@Mearman
Mearman deleted the feat/pdf-codec-decode-budget branch November 25, 2026 09:55
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 5.10.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pdf-codec: a document-wide decode budget, so a conversion's decoding can be bounded before it runs

1 participant